package com.zkk.dao.impl;

import com.zkk.dao.AccountDao;
import com.zkk.pojo.Account;
import com.zkk.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.util.List;

public class AccountDaoImpl implements AccountDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());
    @Override
    public int addAccount(Account account) {
        int update = jdbcTemplate.update("insert into account values (?,?,?,?)", account.getUsername(), account.getId_number(), account.getTelephone(), null);
        return update;
    }

    @Override
    public int setPwd(Account account) {
        int update = jdbcTemplate.update("update account set payment_pwd=? where telephone=?", account.getPayment_pwd(),account.getTelephone());
        return update;
    }

    @Override
    public Account queryAccount(String id_number) {
        List<Account> query = jdbcTemplate.query("select  * from  account where  id_number = ?", new BeanPropertyRowMapper<>(Account.class),id_number);
        if(query.size()==1 & query!=null){
            return query.get(0);
        }
        return null;
    }

    @Override
    public Account queryAccount() {
        List<Account> query = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<>(Account.class));
        if (query.size()==1 & query!=null){
            return query.get(0);
        }
        return null;
    }

    @Override
    public int updateProperty(String id_number,BigDecimal money) {
        int update = jdbcTemplate.update("update account set property = ? where id_number = ?", money, id_number);
        return update;
    }
}
